package singleton.register.containersingleton;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
 * 容器式单例
 * 优点:统一管理对象
 * @author lenovo
 *
 */
public class ContainerSingleton {
	private ContainerSingleton() {
		
	}
	private static Map<String, Object> ioc=new ConcurrentHashMap<>();
	public static Object getBean(String name) {
		synchronized (ioc) {
		if (!ioc.containsKey(name)) {
			Object object=null;
			try {
				object=Class.forName(name).newInstance();
				ioc.put(name, object);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			return object;
		}
		return ioc.get(name);
		}
	}
}
